/* eslint-disable no-unused-vars */
/* eslint-disable no-undef */
import config from '@/config';
/**
 * 显示消息提示框
 * @param content 提示的标题
 */
export function toast(content) {
	uni.showToast({
		icon: 'none',
		title: content,
	});
}

/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
export function showConfirm(content) {
	return new Promise((resolve, reject) => {
		uni.showModal({
			title: '提示',
			content: content,
			cancelText: '取消',
			confirmText: '确定',
			success: function (res) {
				resolve(res);
			},
		});
	});
}

/**
 * 参数处理
 * @param params 参数
 */
export function tansParams(params) {
	let result = '';
	for (const propName of Object.keys(params)) {
		const value = params[propName];
		var part = encodeURIComponent(propName) + '=';
		if (value !== null && value !== '' && typeof value !== 'undefined') {
			if (typeof value === 'object') {
				for (const key of Object.keys(value)) {
					if (
						value[key] !== null &&
						value[key] !== '' &&
						typeof value[key] !== 'undefined'
					) {
						let params = propName + '[' + key + ']';
						var subPart = encodeURIComponent(params) + '=';
						result += subPart + encodeURIComponent(value[key]) + '&';
					}
				}
			} else {
				result += part + encodeURIComponent(value) + '&';
			}
		}
	}
	return result;
}

export const decodeParams = (params) => {
	const obj = {};
	if (params && typeof params === 'object') {
		for (let key in params) {
			obj[key] = decodeURIComponent(params[key]);
		}
		return obj;
	}
	return params;
};

// 获取当前路由参数
export const getRouteParams = () => {
	const pages = getCurrentPages();
	const page = pages[pages.length - 1];
	console.log('page', page);
	console.log('page1', page.route);
	console.log('page2', page.options);
	return page.options;
};

export const getGlobalConfig = () => {
	return getApp().globalData.config;
};

// 通用下载方法
export function downloadExcel(fileName) {
	const filePath =
		config.baseUrl +
		'/common/download?fileName=' +
		encodeURI(fileName) +
		'&delete=' +
		true;
	console.log('filePath', filePath);
	uni.downloadFile({
		url: filePath,
		success: function (res1) {
			console.log('res.downloadFile', res1);
			uni.saveFile({
				tempFilePath: res1.tempFilePath,
				success: function (res2) {
					console.log('res.saveFile', res2);
					uni.openDocument({
						filePath: res2.savedFilePath,
						showMenu: true,
						success: function () {
							console.log('打开文档成功');
						},
					});
				},
			});
		},
	});
}
/**
 * 小数位数，默认2位
 */
export const isDecimalPlaces = (value, places = 2) => {
	const str = `${value}`;
	if (str.indexOf('.') !== -1) {
		const decimal = str.split('.')[1];
		return decimal.length <= places;
	}
	return true;
};
